var ishttps = 'https:' == document.location.protocol ? true : false;
if(ishttps) {
    alert("页面暂时不支持https协议,请切换链接后重试");
    throw SyntaxError();
} 

// 调试模式,强制指定随机码
if((randomID = GetQueryString('room')) == null){
    randomID = (new Date()).valueOf();
}

if(GetQueryString('subscribe') == null){
    // 广播员
    clientId = 'announcer_'+randomID;
}else{
    // 听众
    clientId = 'audience_'+randomID;
}


var subscribeTopic = GetQueryString('subscribe');
var username = 'user';
var password = 'user';
var topic = 'topic/togetherMusic'+clientId;
    topic = hex_md5(topic);
  
var player = new APlayer({
    element: document.getElementById('player'),
    mini: false,
    autoplay: false,
    lrcType: false,
    mutex: true,
    preload: 'metadata',
    listFolded: false,
    listMaxHeight:'400px',
});

// mqtt 配置
var config = {
    /** 用户授权配置 */
    authConfig: {
        // REST API 访问地址 https://console.easemob.com/app/im-service/detail 
        baseUrl : 'baseUrl', 
        // APPKEY https://console.easemob.com/app/applicationOverview/detail 
        orgName : 'orgName', 
        // APPKEY https://console.easemob.com/app/applicationOverview/detail 
        appName : 'appName',    

        clientId: 'clientId',

        clientSecret: 'clientSecret',
    },

    /** 连接配置 */
    connectConfig:{
        //  连接地址 https://console.easemob.com/app/generalizeMsg/overviewService
        host : 'host', 
        // WebSocket 协议服务端口，如果是走 HTTPS，设置443端口
        port: 443 ,     
        // APPID  https://console.easemob.com/app/generalizeMsg/overviewService 
        appId: 'appId',  
        //  用户自定义deviceID
        deviceId :clientId,
        // clientId 格式 deviceID@AppID
        clientId : clientId+'@appId', //

        // 是否走加密 HTTPS，如果走 HTTPS，设置为 true
        useTLS : true, 
        // cleansession标志
        cleanSession : true, 
        // 超时重连时间
        reconnectTimeout : 2000, 
        // 请求超时时间
        timeout: 2, 
        // mqtt版本号
        mqttVersion: 4, 
    },
}

var client = new easemobMQTT(config);


if(subscribeTopic){
    jQuery.getScript("js/client.js");
}else{
    jQuery.getScript("js/server.js");
}


// 连接失败 重连
client.onConnectionFailure = function(){
    client.debugMessage('服务器连接失败');
    client.login(username,password);
    setTimeout(client.connect(), client.connectConfig.reconnectTimeout);
}
// 连接丢失 重连
client.onConnectionLost = function(){
    client.debugMessage('服务器连接已丢失');
    client.login(username,password);
    setTimeout(client.connect(), client.connectConfig.reconnectTimeout);
}

client.login(username,password);
client.connect();


$('input[name=autoPlay]').on('click',function(e){
    if($('input[name=autoPlay]:checked').length>0){
        // 折叠
        $('#primary').fadeOut();        
    }else{
        // 展开
        $('#primary').fadeIn();      
    }
});

